<?php
require_once APPLICATION_PATH . "/models/ModelBase.php";

class Users extends TT_Table_Abstract
{
    protected $_name = 'users';
	protected $_primary = 'uid';
	
	//Ham truy cap vao csdl, kiem tra username va password co ton tai trong csdl khong
	//tra ve object user neu cap username-password do ton tai
	//nguoc lai, tra ve null
	public function checkUser($username, $password)
	{
		$db = $this->getSessionDB();
		$sql = 'SELECT * FROM users WHERE name=? AND pass=?';
		return $db->fetchRow($sql, array($username,$password));		
	}
    
    public function checkLoan($loan_id)      
    {
        $db = $this->getSessionDB();
        $sql = 'SELECT * FROM loans WHERE loan_id=?';
        return $db->fetchRow($sql, array($loan_id));
    }
    
    public function showall()
    {
        $db = $this->getSessionDB();
        $sql = "SELECT * FROM loans WHERE status = '0' ORDER BY loan_id DESC";
        return $db->fetchAll($sql) ;          
    }
    
    public function showall2($page,$page_rows)   // Phan trang
    {
        $page = (int)$page;
        $page_rows = (int)$page_rows;
        $db = $this->getSessionDB();
        $sql = "SELECT * FROM loans WHERE status = '0' ORDER BY loan_id DESC LIMIT $page,$page_rows ";
        return $db->fetchAll($sql) ;          
    }
     public function showall2_num()     // Dem so dong
    {
        $db = $this->getSessionDB();
        $sql = "SELECT count(loan_id) as num FROM loans WHERE status = '0' ORDER BY loan_id DESC ";
        return $db->fetchAll($sql) ;          
    }
    
    public function setPass($uid,$pass)      // Doi mat khau
    {
        $db = $this->getSessionDB();
        $sql = 'UPDATE users SET pass=? WHERE uid=?';
        return $db->query($sql, array($pass,$uid));
    }
}
